<?php

use \GatewayWorker\Lib\Gateway;
class Talking
{
    public static function chat($client_id,$data)
    {
        $data=Tool::ObjectToArray($data);
        $session=Gateway::getSession($client_id);
        $group_name=Service::get_seeion_group($session);
        $send=[
            "message_id"=>$group_name,
            "uid"=>Gateway::getUidByClientId($client_id),
            "content"=>$data["content"],
            "msg_type"=>$data["msg_type"],
            "message"=>$data["message"],
            "url"=>$data["url"],
            "cid"=>$data["cid"]
        ];

        $line=Gateway::isUidOnline($data["cid"]);
        print_r("line:".$line);
        if ($line==0){
            $curl_message=[
                "id"=>$data["cid"],
                "token"=>$session["token"],
                "message"=>$data["message"],
                "url"=>$data["url"]
            ];
            $message_url=Tool::$http_url."send_message";
            print_r($curl_message);
            print_r($message_url);

            (new Tool())->curl_post($message_url,$curl_message);
        }


        $url=Tool::$http_url."user/common/save_chat";
        (new Tool())->curl_post($url,$send);
        $send_data=[
            "type"=>"msg_reg",
            "data"=>$send
        ];
        $send_data=json_encode($send_data);
        Gateway::sendToGroup($group_name,$send_data);


    }
}
